<?php

namespace App\Console\Commands\Cruise;

use App\Library\FeiShu\SendFs;
use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Console\Command;
use Kuafu\GeneralFs\BuildFs;

class CruiseCheckRectifyNotify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:cruiseCheckRectifyNotify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '超期未整改飞书机器人消息提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 文档 https://onepiece-group.feishu.cn/wiki/SGA8wKnMEiyCFBkgZPwcSpZfnTd
     * @return int
     */
    public function handle()
    {
        $deadlineTime      = time();                                                // 截止处理时间
        $deadlineOneTime   = strtotime(date('Y-m-d 23:59:59', strtotime('+1 day')));// 距离整改截止时间一天内的
        $deadlineThreeTime = strtotime(date('Y-m-d 23:59:59', strtotime('+3 day')));// 距离整改截止时间三天内的

        $resOne = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_SUBMIT, $deadlineTime, 0);

        $rectifyShopCount = $resOne['shopTotal'];    // 在整改截止时间内的,待整改的门店数量
        $rectifyCount     = $resOne['rectifyTotal']; // 在整改截止时间内的,待整改的项数量

        $resTwo = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_AUDIT, $deadlineTime, 0);

        $rectifyAuditShopCount = $resTwo['shopTotal'];    // 在整改截止时间内的,门店提交整改且待审核的门店数量
        $rectifyAuditCount     = $resTwo['rectifyTotal']; // 在整改截止时间内的,门店提交整改且待审核的项数量

        $resThree = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_SUBMIT, $deadlineTime, $deadlineOneTime);

        $rectifyOneShopCount = $resThree['shopTotal'];    // 距离整改截止时间一天内的,门店待整改的门店数量
        $rectifyOneCount     = $resThree['rectifyTotal']; // 距离整改截止时间一天内的,门店待整改的项数量

        $resFour = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_AUDIT, $deadlineTime, $deadlineOneTime);

        $rectifyOneAuditShopCount = $resFour['shopTotal'];    // 距离整改截止时间一天内的,门店提交整改且待审核的门厂店数量
        $rectifyOneAuditCount     = $resFour['rectifyTotal']; // 距离整改截止时间一天内的,门店提交整改且待审核的项数量

        $resFive = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_SUBMIT, $deadlineTime, $deadlineThreeTime);

        $rectifyThreeShopCount = $resFive['shopTotal'];    // 距离整改截止时间三天内的,门店待整改的门店数量
        $rectifyThreeCount     = $resFive['rectifyTotal']; // 距离整改截止时间三天内的,门店待整改的项数量

        $resSix = CruiseCheckRectifyModel::getData(CruiseTemplateModel::TYPE_3, CruiseCheckRectifyModel::STATUS_WAIT_AUDIT, $deadlineTime, $deadlineThreeTime, 1);

        $rectifyThreeAuditShopCount = $resSix['shopTotal'];         // 距离整改截止时间三天内的,门店提交整改且待审核的门厂店数量
        $rectifyThreeAuditCount     = $resSix['rectifyTotal'];      // 距离整改截止时间三天内的,门店提交整改且待审核的项数量
        $rectifyThreeShopList       = $resSix['rectifyShopList'];   // 临期3天未整改门店明细(只显示20条) 整改期内，显示临期3天，整改率不足60%，有待审核整改单的门店

        $templateData = [
            'startTime'                  => date('Y-m-d', $deadlineTime),
            'rectifyShopCount'           => $rectifyShopCount,
            'rectifyAuditShopCount'      => $rectifyAuditShopCount,
            'rectifyThreeShopCount'      => $rectifyThreeShopCount,
            'rectifyThreeAuditShopCount' => $rectifyThreeAuditShopCount,
            'rectifyThreeShopList'       => $rectifyThreeShopList,
            'rectifyCount'               => $rectifyCount,
            'rectifyAuditCount'          => $rectifyAuditCount,
            'rectifyThreeCount'          => $rectifyThreeCount,
            'rectifyThreeAuditCount'     => $rectifyThreeAuditCount,
            'rectifyOneShopCount'        => $rectifyOneShopCount,
            'rectifyOneCount'            => $rectifyOneCount,
            'rectifyOneAuditShopCount'   => $rectifyOneAuditShopCount,
            'rectifyOneAuditCount'       => $rectifyOneAuditCount,
        ];
        dump($templateData['startTime']);

        $templateJson = view()->file(app_path('Remote/Feishu/FsTemplate/Cruise/CruiseRectifyFsTemplate.blade.php'), $templateData)->render();

        // 飞书机器人推送
        BuildFs::newRobot(SendFs::ROBOT_CHECK_RECTIFY, BuildFs::MSG_TYPE_CARD)
            ->setCardContent(json_decode($templateJson, true))
            ->send();

        // 飞书机器人推送 -2群
        BuildFs::newRobot(SendFs::ROBOT_CHECK_RECTIFY_TWO, BuildFs::MSG_TYPE_CARD)
            ->setCardContent(json_decode($templateJson, true))
            ->send();
    }
}
